package com.jsoft.service;

import com.jsoft.dto.SectorDto;
import com.jsoft.manager.DepartmentManager;
import com.jsoft.manager.SectorManager;
import com.jsoft.manager.UserManager;
import com.jsoft.model.DepartmentDao;
import com.jsoft.model.SectorDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 16.08.13
 * Time: 14:10
 */

@Service
public class SectorServiceImpl implements SectorService {
    @Autowired
    private SectorManager sectorManager;
    @Autowired
    private UserManager userManager;
    @Autowired
    private DepartmentManager departmentManager;

    @Override
    @Transactional(readOnly = true)
    public List<SectorDto> getSectorList() {
        List<SectorDao> daoList = sectorManager.getAllList();
        List<SectorDto> sectorDtoList = new ArrayList<SectorDto>();
        for (SectorDao dao : daoList) {
            sectorDtoList.add(dao.wrapSectorDto());
        }
        return sectorDtoList;
    }

    @Override
    public List<SectorDto> getSectorListByUserId(Integer userId) {
        List<SectorDao> sectorDaoList = userManager.getById(userId).getSectorList();
        List<SectorDto> sectorDtoList = new ArrayList<SectorDto>();
        for (SectorDao dao : sectorDaoList) {
            sectorDtoList.add(dao.wrapSectorDto());
        }
        return sectorDtoList;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SectorDto> getSectorListByDepartmentId(Integer departmentId) {
        List<SectorDao> sectorDaoList = departmentManager.getById(departmentId).getSectorList();
        List<SectorDto> sectorDtoList = new ArrayList<SectorDto>();
        for (SectorDao dao : sectorDaoList) {
            sectorDtoList.add(dao.wrapSectorDto());
        }
        return sectorDtoList;
    }
}
